package ro.isdc.vms.vehicleadvertisement.batch.writers;

import java.util.List;

import org.springframework.batch.item.ItemWriter;

import ro.isdc.vms.utils.timestamp.TimeStampUtil;
import ro.isdc.vms.vehicleadvertisement.dao.VehicleAdvertisementDAO;
import ro.isdc.vms.vehicleadvertisement.domain.VehicleAdvertisement;

/**
 * Item writer used for flagging old advertisements which also encapsulates
 * flagging logic.
 * 
 * @author Andrei Chirila
 */
public class FlagOldAdvertisementsItemWriter implements ItemWriter<VehicleAdvertisement> {
	private VehicleAdvertisementDAO vehicleAdvertisementDAO;
	
	/**
	 * @param vehicleAdvertisementDAO the vehicleAdvertisementDAO to set
	 */
	public void setVehicleAdvertisementDAO(
			final VehicleAdvertisementDAO vehicleAdvertisementDAO) {
		this.vehicleAdvertisementDAO = vehicleAdvertisementDAO;
	}

	/**
	 * {@inheritDoc}
	 */
	public void write(List<? extends VehicleAdvertisement> advertisements)
			throws Exception {
		for (VehicleAdvertisement va: advertisements) {
			va.setFlaggedAsOld(true);
			va.setDeletionTimestamp(TimeStampUtil.getTimeStamp());
			vehicleAdvertisementDAO.update(va);
		}
	}
}
